import { createI18n } from 'vue-i18n'
import zhCN from './zh-CN'
import enUS from './en-US'

// 支持的语言列表
export const SUPPORTED_LOCALES = ['zh-CN', 'en-US'] as const
export type SupportedLocale = typeof SUPPORTED_LOCALES[number]

// 语言显示名称映射
export const LOCALE_NAMES: Record<SupportedLocale, string> = {
  'zh-CN': '中文',
  'en-US': 'English'
}

// 从localStorage获取保存的语言设置，默认为英文
const getStoredLocale = (): SupportedLocale => {
  const stored = localStorage.getItem('locale') as SupportedLocale
  const defaultLocale = 'en-US'
  
  if (!stored) {
    console.log('[I18n] No language preference found, using default: English')
  }
  
  return stored && SUPPORTED_LOCALES.includes(stored) ? stored : defaultLocale
}

// 保存语言设置到localStorage
export const setStoredLocale = (locale: SupportedLocale) => {
  localStorage.setItem('locale', locale)
}

// 创建I18n实例
const i18n = createI18n({
  legacy: false,
  locale: getStoredLocale(),
  fallbackLocale: 'en-US',
  messages: {
    'zh-CN': zhCN,
    'en-US': enUS
  }
})

console.log('[I18n] I18n instance created with locale:', i18n.global.locale.value)

export default i18n 